package com.example.administrator.managesystem.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;

import com.example.administrator.managesystem.utils.ToastUtils;

/**
 * Created by Administrator on 2018/12/14.
 */
/*
* 自定义接收器
*/

public class MyNetStatusReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取移动网连接状态
        NetworkInfo mobileNetworkInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        //获取wifi连接状态
        NetworkInfo wifiNetworkInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (!mobileNetworkInfo.isConnected() && !wifiNetworkInfo.isConnected()) {
            ToastUtils.showToast(context,"网络走丢了...");
//            // 如果没有网络连接
//            new AlertDialog.Builder(context).setTitle("网络错误")
//                    .setMessage("网络连接失败，请确认网络连接")
//                    .setPositiveButton("知道了", null).setCancelable(true).show();
        } else {
//            new AlertDialog.Builder(context).setTitle("网络连接")
//                    .setMessage("网络连接成功").setPositiveButton("知道了", null)
//                    .setCancelable(true).show();

        }
    }
}
